home *** CD-ROM | disk | FTP | other *** search
/ Aminet 2 / Aminet AMIGA CDROM (1994)(Walnut Creek)[Feb 1994][W.O. 44790-1].iso / Aminet / mus / play / tracker_3_19.lzh / tracker / player.c < prev    next >
C/C++ Source or Header  |  1993-11-19  |  9KB  |  436 lines

  1. /* player.c */
  2.  
  3. /* $Id: player.c,v 3.17 1993/11/19 14:27:06 espie Exp espie $
  4.  * $Log: player.c,v $
  5.  * Revision 3.17  1993/11/19  14:27:06  espie
  6.  * Stupid bug.
  7.  *
  8.  * Revision 3.16  1993/11/17  15:31:16  espie
  9.  * New high-level functions.
  10.  *
  11.  * Revision 3.15  1993/11/11  20:00:03  espie
  12.  * Amiga support.
  13.  *
  14.  * Revision 3.14  1993/08/04  11:34:33  espie
  15.  * *** empty log message ***
  16.  *
  17.  * Revision 3.13  1993/07/18  11:49:29  espie
  18.  * Bug with delay_pattern: can't factorize the check for effect thingy.
  19.  *
  20.  * Revision 3.12  1993/07/18  10:39:44  espie
  21.  * Added forking under unix.
  22.  *
  23.  *
  24.  * Revision 3.10  1993/05/09  14:06:03  espie
  25.  * Reniced verbose output display.
  26.  *
  27.  * Revision 3.9  1993/04/25  14:08:15  espie
  28.  * Bug fix: now use correct finetune when loading samples/starting notes.
  29.  *
  30.  * Revision 3.8  1993/01/15  14:00:28  espie
  31.  * Added bg/fg test.
  32.  *
  33.  * Revision 3.6  1992/11/27  10:29:00  espie
  34.  * General cleanup
  35.  *
  36.  * Revision 3.5  1992/11/24  10:51:19  espie
  37.  * un#ifdef'ed showseq code.
  38.  *
  39.  * Revision 3.3  1992/11/22  17:20:01  espie
  40.  * Added <> operators.
  41.  * Added update frequency on the fly.
  42.  *
  43.  * Revision 3.2  1992/11/20  14:53:32  espie
  44.  * Added finetune.
  45.  *
  46.  * Revision 3.1  1992/11/19  20:44:47  espie
  47.  * Protracker commands.
  48.  *
  49.  * Revision 3.0  1992/11/18  16:08:05  espie
  50.  * New release.
  51.  *
  52.  * Revision 2.19  1992/11/17  17:15:37  espie
  53.  * Added interface using may_getchar(). Still primitive, though.
  54.  * imask, start.
  55.  * Added transpose feature.
  56.  * Added possibility to get back to MONO for the sgi.
  57.  * Added stereo capabilities to the indigo version.
  58.  * Added two level of fault tolerancy.
  59.  * Added some control on the number of replays,
  60.  * and better error recovery.
  61.  */
  62.      
  63. #include <stdio.h>
  64.      
  65. #include "defs.h"
  66. #include "extern.h"
  67. #include "song.h"
  68. #include "channel.h"
  69. #include "pref.h"
  70.      
  71.  
  72. LOCAL char *id = "$Id: player.c,v 3.17 1993/11/19 14:27:06 espie Exp espie $";
  73.      
  74.  
  75. /* setting up a given note */
  76.  
  77. void reset_note(ch, note, pitch)
  78. struct channel *ch;
  79. int note;
  80. int pitch;
  81.     {
  82.         /*
  83.     ch->pointer = 0;
  84.     ch->mode = PLAY;
  85.     set_current_pitch(ch, pitch);
  86.         */
  87.     ch->pitch = pitch;
  88.     ch->note = note;
  89.     ch->viboffset = 0;
  90.     play_note(ch->audio, ch->samp, pitch);
  91.     }
  92.  
  93. /* changing the current pitch (value
  94.  * may be temporary, and not stored
  95.  * in channel pitch, for instance vibratos.
  96.  */
  97. void set_current_pitch(ch, pitch)
  98. struct channel *ch;
  99. int pitch;
  100.     {
  101.         /* save current pitch in case we want to change
  102.          * the step table on the run
  103.     ch->cpitch = pitch;
  104.     ch->step = step_table[pitch];
  105.         */
  106.     set_play_pitch(ch->audio, pitch);
  107.     }
  108.  
  109. /* changing the current volume. You HAVE to get through
  110.  * there so that it will work on EVERY machine.
  111.  */
  112. void set_current_volume(ch, volume)
  113. struct channel *ch;
  114. int volume;
  115.     {
  116.     ch->volume = MAX(MIN(volume, MAX_VOLUME), MIN_VOLUME);
  117.     set_play_volume(ch->audio, volume);
  118.     }
  119.  
  120. void set_position(ch, pos)
  121. struct channel *ch;
  122. int pos;
  123.     {
  124.     set_play_position(ch->audio, pos);
  125.     /*
  126.     ch->pointer = int_to_fix(pos);
  127.     */
  128.     }
  129.  
  130. /* init_channel(ch, dummy):
  131.  * setup channel, with initially
  132.  * a dummy sample ready to play,
  133.  * and no note.
  134.  */
  135. LOCAL void init_channel(ch)
  136. struct channel *ch;
  137.     {
  138.     ch->samp = NULL;
  139.     ch->finetune = 0;
  140.     ch->audio = new_channel();
  141.     ch->volume = 0; 
  142.     ch->pitch = 0; 
  143.     ch->note = NO_NOTE;
  144.  
  145.         /* we don't setup arpeggio values. */
  146.     ch->viboffset = 0; 
  147.     ch->vibdepth = 0;
  148.  
  149.     ch->slide = 0; 
  150.  
  151.     ch->pitchgoal = 0; 
  152.     ch->pitchrate = 0;
  153.  
  154.     ch->volumerate = 0;
  155.  
  156.     ch->vibrate = 0;
  157.     ch->adjust = do_nothing;
  158.     }
  159.  
  160.  
  161.  
  162. LOCAL int VSYNC;          /* base number of sample to output */
  163. void (*eval[NUMBER_EFFECTS])();
  164.                     /* the effect table */
  165. LOCAL int oversample;     /* oversample value */
  166. LOCAL int frequency;      /* output frequency */
  167. LOCAL int channel;        /* channel loop counter */
  168.  
  169. LOCAL struct channel chan[NUMBER_TRACKS];
  170.                     /* every channel */
  171. LOCAL int countdown;      /* keep playing the tune or not */
  172.  
  173. LOCAL struct song_info *info;
  174. LOCAL struct sample_info *voices;
  175.  
  176. LOCAL struct automaton a;
  177.  
  178.  
  179. void init_player(o, f)
  180. int o, f;
  181.     {
  182.     oversample = o;
  183.     frequency = f;
  184.     init_tables(o, f, NULL);
  185.     init_effects(eval);
  186.     init_display();
  187.     }
  188.  
  189. LOCAL void setup_effect(ch, a, e, imask, bcdvol)
  190. struct channel *ch;
  191. struct automaton *a;
  192. struct event *e;
  193. unsigned long imask;
  194. int bcdvol;
  195.     {
  196.     int samp, cmd;
  197.  
  198.         /* retrieves all the parameters */
  199.     samp = e->sample_number;
  200.  
  201.         /* load new instrument */
  202.     if (samp)  
  203.         {
  204.             /* note that we can change sample in the middle
  205.              * of a note. This is a *feature*, not a bug (see
  206.              * made). Precisely: the sampel change will be taken
  207.              * into account for the next note, BUT the volume change
  208.              * takes effect immediately.
  209.              */
  210.         ch->samp = voices + samp;
  211.         ch->finetune = voices[samp].finetune;
  212.         if ((1L<<samp) & imask)
  213.             ch->samp = voices;
  214.         set_current_volume(ch, voices[samp].volume);
  215.         }
  216.  
  217.     a->note = e->note;
  218.     if (a->note != NO_NOTE)
  219.         a->pitch = pitch_table[a->note][ch->finetune];
  220.     else
  221.         a->pitch = e->pitch;
  222.     cmd = e->effect;
  223.     a->para = e->parameters;
  224.  
  225.     if (a->pitch >= MAX_PITCH)
  226.         {
  227.         fprintf(stderr, "Pitch out of bounds %d\n", a->pitch);
  228.         a->pitch = 0;
  229.         error = FAULT;
  230.         }
  231.  
  232.     if (show && run_in_fg())
  233.         dump_event(ch, e, imask);
  234.         /* check for a new note: portamento
  235.          * is the special case where we do not restart
  236.          * the note.
  237.          */
  238.     if (a->pitch && cmd != EFF_PORTA && cmd != EFF_PORTASLIDE)
  239.         reset_note(ch, a->note, a->pitch);
  240.     ch->adjust = do_nothing;
  241.         /* do effects */
  242.     (eval[cmd])(a, ch);
  243.     }
  244.  
  245.  
  246. LOCAL void adjust_sync(ofreq, tempo)
  247. int ofreq, tempo;
  248.     {
  249.     VSYNC = ofreq * NORMAL_FINESPEED / tempo;
  250.     }
  251.  
  252. LOCAL void play_once(a, pref)
  253. struct automaton *a;
  254. struct pref *pref;
  255.     {
  256.     int channel;
  257.  
  258.     if (a->do_stuff & DELAY_PATTERN)
  259.         for (channel = 0; channel < NUMBER_TRACKS; channel++)
  260.             /* do the effects */
  261.             (chan[channel].adjust)(chan + channel);
  262.     else
  263.         {    
  264.         if (a->counter == 0)
  265.             for (channel = 0; channel < NUMBER_TRACKS; channel++)
  266.                 /* setup effects */
  267.                 setup_effect(chan + channel, a, 
  268.                     &(a->pattern->e[channel][a->note_num]), 
  269.                     pref->imask, pref->bcdvol);
  270.         else
  271.             for (channel = 0; channel < NUMBER_TRACKS; channel++)
  272.                 /* do the effects */
  273.                 (chan[channel].adjust)(chan + channel);
  274.         }
  275.  
  276.         /* advance player for the next tick */
  277.     next_tick(a);
  278.         /* actually output samples */
  279.     resample(chan, oversample, VSYNC / a->finespeed);
  280.     }
  281.  
  282.  
  283.  
  284. void play_song(song, pref, start)
  285. struct song *song;
  286. struct pref *pref;
  287. int start;
  288.     {
  289.     int tempo;
  290.  
  291.     tempo = pref->speed;
  292.  
  293.     adjust_sync(frequency, tempo);
  294.     /* a repeats of 0 is infinite replays */
  295.     if (pref->repeats)
  296.         countdown = pref->repeats;
  297.     else
  298.         countdown = 1;
  299.  
  300.     info = &song->info;
  301.     voices = song->samples; 
  302.  
  303.     init_automaton(&a, song, start);
  304.  
  305.     no_audio_channels();
  306.  
  307.     for (channel = 0; channel < NUMBER_TRACKS; channel++) 
  308.         init_channel(chan + channel);
  309.  
  310.  
  311.     while(countdown)
  312.         {
  313.         play_once(&a, pref);
  314.         switch(may_getchar())
  315.             {
  316.         case 'n':
  317. #ifdef FORKING
  318.         if (forked)
  319.             break;
  320.         forked = TRUE;
  321.         if (pid = fork())
  322.             {
  323.             nice(15);
  324.             error = NEXT_SONG;
  325.             return;
  326.             }
  327.         else
  328.             break;
  329. #else
  330.             discard_buffer();
  331.             error = NEXT_SONG;
  332.             return;
  333. #endif
  334.         case 'p':
  335. #ifdef FORKING
  336.         if (forked)
  337.             break;
  338.         forked = TRUE;
  339.         if (pid = fork())
  340.             {
  341.             nice(15);
  342.             error = PREVIOUS_SONG;
  343.             return;
  344.             }
  345.         else 
  346.             break;
  347. #else
  348.             discard_buffer();
  349.             error = PREVIOUS_SONG;
  350.             return;
  351. #endif
  352.         case 'x':
  353.         case 'e':
  354.         case 'q':
  355.             discard_buffer();
  356.             end_all();
  357.         case 's':
  358.             tempo = 50;
  359.             adjust_sync(frequency, tempo);
  360.             break;
  361.         case 'S':
  362.             tempo = 60;
  363.             adjust_sync(frequency, tempo);
  364.             break;
  365.         case 'r':
  366.             discard_buffer();
  367.             init_automaton(&a, song, start);
  368.             no_audio_channels();
  369.             for (channel = 0; channel < NUMBER_TRACKS; channel++) 
  370.                 init_channel(chan + channel);
  371.             break;
  372.         case '>':
  373.             discard_buffer();
  374.             init_automaton(&a, song, a.pattern_num+1);
  375.             break;
  376.         case '<':
  377.             discard_buffer();
  378.             if (a.note_num < 4)
  379.                 init_automaton(&a, song, a.pattern_num -1);
  380.             else
  381.                 init_automaton(&a, song, a.pattern_num);
  382.             break;
  383.         case '?':
  384.             dump_song(song);
  385.             show = TRUE;
  386.             break;
  387.         case '!':
  388.             show = FALSE;
  389.             break;
  390.         case ' ':
  391.             while (may_getchar() == EOF)
  392.                 ;
  393.             break;
  394.         default:
  395.             break;
  396.             }
  397.         {
  398.         int new_freq;
  399.         if (new_freq = update_frequency())
  400.             {
  401.             frequency = new_freq;
  402.             adjust_sync(frequency, tempo);
  403.             init_tables(oversample, frequency, chan);
  404.             }
  405.         }
  406.  
  407.         switch(error)
  408.             {
  409.         case NONE:
  410.             break;
  411.         case ENDED:
  412.             if (pref->repeats)
  413.                 countdown--;
  414.             break;
  415.         case SAMPLE_FAULT:
  416.             if (!pref->tolerate)
  417.                 countdown = 0;
  418.             break;
  419.         case FAULT:
  420.             if (pref->tolerate < 2)
  421.                 countdown = 0;
  422.             break;
  423.         case PREVIOUS_SONG:
  424.         case NEXT_SONG:
  425.         case UNRECOVERABLE:
  426.             countdown = 0;
  427.             break;
  428.         default:
  429.             break;
  430.             }
  431.         error = NONE;
  432.         }
  433.          
  434.     }
  435.  
  436.